package com.davesandoval.tpdroid;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class SoViewActivity extends ListActivity {

	private Cursor mCursor;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		System.out.println("------------SOViewActivity onCreate--------");
		setContentView(R.layout.soview);
		if (!Main.dbHelper.isOpen()) {
			Main.dbHelper.open();
		}
		mCursor = Main.dbHelper.fetchAllSO();
		startManagingCursor(mCursor);
		mCursor.moveToFirst();

		Context ctx = getApplicationContext();
		setListAdapter(new ExtendedCursorAdapter(ctx, mCursor));
	}//----End onCreate----

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Intent intent = new Intent(this, SODetailsActivity.class);
		intent.putExtra("position", position);
		startActivityForResult(intent, 1);
		super.onListItemClick(l, v, position, id);
	}

	@Override
	protected void onResume() {
		System.out.println("------------SOViewActivity onResume--------");
		Main.dbHelper.open();
		mCursor = Main.dbHelper.fetchAllSO();
		startManagingCursor(mCursor);
		super.onResume();
	}

	@Override
	protected void onStop() {
		System.out.println("------------SOViewActivity onStop--------");
		stopManagingCursor(mCursor);
		super.onStop();
	}

	@Override
	protected void onPause() {
		System.out.println("------------SOViewActivity onPause--------");
		stopManagingCursor(mCursor);
		super.onPause();
	}
	
	public class ExtendedCursorAdapter extends CursorAdapter {
		public ExtendedCursorAdapter(Context context, Cursor c) {
			super(context, c);
		}
		
		@Override
		public void bindView(View v, Context ctx, Cursor c) {
			TextView soNumber = (TextView) v.findViewById(R.id.soNumber);
			soNumber.setText(c.getString(c.getColumnIndex(TPdbAdapter.KEY_SO_NUM)));
			TextView status = (TextView) v.findViewById(R.id.status);
			status.setText(c.getString(c.getColumnIndex(TPdbAdapter.KEY_STATUS)));
			if (status.getText().toString().equalsIgnoreCase("Open")) {
				status.setTextColor(Color.GREEN);
			} else if (status.getText().toString().equalsIgnoreCase("Dispatched")) {
				status.setTextColor(Color.YELLOW);
			}else if (status.getText().toString().equalsIgnoreCase("Completed")) {
				status.setTextColor(Color.RED);
			}
			TextView accName = (TextView) v.findViewById(R.id.accName);
			accName.setText(c.getString(c.getColumnIndex(TPdbAdapter.KEY_ACCOUNT_NAME)));
			TextView soBriefDescription = (TextView) v.findViewById(R.id.soBriefDescription);
			soBriefDescription.setText(c.getString(c.getColumnIndex(TPdbAdapter.KEY_BRIEF_DESCRIPTION)));
		}
		
		@Override
		public View newView(Context ctx, Cursor c, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(ctx);
			View v = inflater.inflate(R.layout.so_row, parent, false);
			bindView(v, ctx, c);
			return v;
		}
	}
}
